<?php

class Xurl {

    public $short_url;
    public $expanded_url;
    public $url_meta;
    public $response_meta;
    private $header_info;
    
    public function __construct($short_url, $args = array()) {
        $this->short_url = $short_url;
        $this->get_http_response($short_url, $args);
        $this->set_data();
    }

    private function get_http_response($url, $curlopt = array()) {
        $ch = curl_init();
        $default_curlopt = array(
            CURLOPT_TIMEOUT => 2,
            CURLOPT_FILETIME => 1,
            CURLOPT_NOBODY => 1,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_MAXREDIRS => 5,
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_USERAGENT => 'xURL/0.1'
        );
        $curlopt = array(CURLOPT_URL => $url) + $curlopt + $default_curlopt;
        curl_setopt_array($ch, $curlopt);
        curl_exec($ch);
        $this->header_info = curl_getinfo($ch);
        curl_close($ch);
    }

    private function set_data() {
        $header_info = $this->header_info;
        $url_info = parse_url($header_info['url']);
        $url_info['query_clean'] = parse_url($this->clean_url($header_info['url']), PHP_URL_QUERY);
        if (!empty($url_info['path']))
            $url_info['basename'] = basename($url_info['path']);

        $this->expanded_url->raw = $header_info['url'];
        $this->expanded_url->clean = $this->clean_url($header_info['url']);

        $this->response_meta->content_type = $header_info['content_type'];
        $this->response_meta->http_code = $header_info['http_code'];
        $this->response_meta->redirect_count = $header_info['redirect_count'];
        $this->response_meta->total_time = $header_info['total_time'];

        $this->url_meta->host = $url_info['host'];
        if (!empty($url_info['path']))
            $this->url_meta->path = $url_info['path'];
        if (!empty($url_info['basename']))
            $this->url_meta->basename = $url_info['basename'];
        if (!empty($url_info['query']))
            $this->url_meta->query = $url_info['query'];
        if (!empty($url_info['query_clean']))
            $this->url_meta->query_clean = $url_info['query_clean'];
        if (!empty($url_info['fragment']))
            $this->url_meta->fragment = $url_info['fragment'];

        unset($this->header_info);
    }

    public function get_data($output_type = 'object') {
        $output_type = strtolower($output_type);
        if ($output_type == 'object')
            return $this;
        if ($output_type == 'array')
            return array(
                'short_url' => $this->short_url,
                'expanded_url' => get_object_vars($this->expanded_url),
                'response_meta' => get_object_vars($this->response_meta),
                'url_meta' => get_object_vars($this->url_meta)
            );
        if ($output_type == 'json')
            return json_encode($this);
    }

    private function clean_url($url) {
        $url = trim($url);
        $url_info = parse_url($url);
        if (empty($url_info['query']))
            return $url;
        $query = $url_info['query'];
        parse_str($query, $query_array);
        $unset_vars = array('utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign');
        foreach ($unset_vars as $unset_var)
            unset($query_array[$unset_var]);
        $new_query = http_build_query($query_array);
        $url = str_replace($query, $new_query, $url);
        if (substr($url, -1) == '?')
            return substr($url, 0, (strlen($url) - 1));
    }

}

// Usage
$xurl = new Xurl('http://t.co/Wytzk8Yb');
// As Object
echo (string)$xurl->get_data('object')->expanded_url->raw;
// As Array
$array = $xurl->get_data('array');
echo $array['expanded_url']['raw'];
// As JSON
$json = json_decode($xurl->get_data('json'));
echo $json->expanded_url->raw;

?>